const state = {
  count:333,
  cart:[]
}

const mutations = {
  ADD_CART (state, paylload) {
    let flag = false
    state.cart.forEach(item => {
      if (item.id === paylload.id) {
        item.count += paylload.count
        flag = true
      }
    })
    if (!flag) {
      state.cart.push(paylload)
    }
    localStorage.cart = JSON.stringify(state.cart)
  },
  setAllChbox (state) {
    state.cart.forEach(item => {
      item.completed = true
    })
  }
}

const actions = {

}

const getters = {
  addallcart (state) {
    var o = 0
    state.cart.forEach(item => {
      o += item.count
    })
    return o
  },
  addprices (state) {
    var c = 0
    state.cart.forEach(item => {
      if (item.completed) {
        c += (item.price * item.count)
      }
    })
    return c
  },
  allCheck (state) {
    return state.cart.every(item => item.completed)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}
